<?php

use think\facade\Db;


function get_pay_way($value)
{
    return empty($value) ? '未支付' : match ($value) {
        'alipay' => '支付宝',
        'wechat' => '微信支付',
        default => \app\common\model\Assets::getFields(3)[$value]
    };
}


function getBonusTitle($type)
{
    if ($type == 1) {
        return [1 => '直推奖', 2 => '团队奖', 3 => '平级奖', 4 => '业绩分红', 5 => '加权分红'];
    } else {
        return ['sum_1' => '直推奖', 'sum_2' => '团队奖', 'sum_3' => '平级奖', 'sum_4' => '业绩分红', 'sum_5' => '加权分红'];
    }
}

/**
 * 整理菜单树方法
 * @param $param
 * @return array
 */
function prepareMenu($param, $pid = 1)
{

    $parent = []; //父类
    $child = [];  //子类
    foreach ($param as $key => $vo) {

        if ($vo['pid'] == $pid) {
            $vo['href'] = '#';
            $parent[] = $vo;
        } else {
            $vo['href'] = strstr($vo['name'], '/') ? url($vo['name'])->build() : url($vo['name'] . '/index')->build();
            $child[] = $vo;
        }
    }

    foreach ($parent as $key => $vo) {
        foreach ($child as $k => $v) {

            if ($v['pid'] == $vo['id']) {

                $parent[$key]['child'][] = $v;
            }
        }
    }

    unset($child);
    return $parent;
}

function getCount($fetchSql)
{
    $fetchSql = "SELECT count(*) as tp_count " . strstr($fetchSql, "FROM");
    return Db::query($fetchSql)[0]['tp_count'];
}

function generate($length, $prefix = '', $type = 1)
{
    $code = \think\helper\Str::random($length, $type, $prefix);
    $is = \app\common\model\User::where('code', $code)->count();
    return $is ? generate(6, $prefix, $type) : $code;
}

function generateRandom($length)
{
    return \think\helper\Str::random($length);
}


//格式化富文本内容
function handleRichText($content)
{
    $url = request()->scheme() . '://' . $_SERVER['SERVER_NAME'] . '/uploads/';
    $content = htmlspecialchars_decode(str_replace('/uploads/', $url, $content));
    return str_replace('\\', '', $content);
}

function handleImg($src)
{
    if (empty($src)) {
        return '';
    }
    if (is_array($src)) {
        foreach ($src as &$v) {
            $v = request()->scheme() . '://' . $_SERVER['SERVER_NAME'] . $v;
        }
        return $src;
    } else {
        return request()->scheme() . '://' . $_SERVER['SERVER_NAME'] . $src;
    }
}

function filter_Emoji($str)
{
    $str = preg_replace_callback(    //执行一个正则表达式搜索并且使用一个回调进行替换
        '/./u',
        function (array $match) {
            return strlen($match[0]) >= 4 ? '' : $match[0];
        },
        $str
    );

    return $str;
}

function queryIp($ip)
{

    $url = "http://whois.pconline.com.cn/jsFunction.jsp?ip=" . $ip;
    $ch = curl_init();
    //设置选项，包括URL
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    //执行并获取HTML文档内容
    $output = curl_exec($ch);
    //释放curl句柄
    curl_close($ch);
    $info = iconv('GB2312', 'UTF-8', $output); //因为是js调用 所以接收到的信息为字符串，注意编码格式

    return get_between($info, "('", "',");
}

function get_between($input, $start, $end)
{
    return substr($input, strlen($start) + strpos($input, $start), (strlen($input) - strpos($input, $end)) * (-1));
}

/***
 * 时间转秒
 */
function time_to_sec($time)
{
    list($h, $i) = explode(':', $time);
    return $h * 3600 + $i * 60;
}
/**
 * 隐藏真实姓名
 * @param string $name 真实姓名
 * @return string
 */
function getHideRealName($name)
{

    $length = mb_strlen($name, 'utf-8');
    if ($length == 2) {
        $hide_str = mb_substr($name, 0, 1, 'utf-8') . '*';
    } elseif ($length >= 3) {
        $hide_str = mb_substr($name, 0, 1, 'utf-8') . '*' . mb_substr($name, -1, 1, 'utf-8');
    } else {
        $hide_str = $name;
    }
    return $hide_str;
}
/**
 * 判断中奖的数据
 */
function get_prize($data, $win, $fail)
{
    shuffle($data); // 使用shuffle函数随机排序号码  
    $numbers = $data;
    // 设置奖项
    $firstPrizeCount = floor(count($numbers) * $win); // 绿色权益
    $secondPrizeCount = floor(count($numbers) * $fail); // 红色权益
    // 分配奖项
    $firstPrizeNumbers = array_slice($numbers, 0, $firstPrizeCount); // 绿色权益
    $secondPrizeNumbers = array_slice($numbers, $firstPrizeCount, $secondPrizeCount); // 红色权益
    $unwonNumbers = array_slice($numbers, $firstPrizeCount + $secondPrizeCount); // 黄色权益
    return [
        'win' => $firstPrizeNumbers,
        'fail' => $secondPrizeNumbers,
        'take' => $unwonNumbers,
    ];
}
